package com.example.demo.controller;

import com.example.demo.entity.Recreation;
import com.example.demo.service.RecreationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/recreation")
public class RecreationController {

    private RecreationService recreationService;
    @Autowired
    public void setRecreationService(RecreationService recreationService){
        this.recreationService=recreationService;
    }

    @GetMapping("/getAll")
    public List<Recreation> getAll(){
        System.out.println("=====get====");
        List<Recreation> list=recreationService.getAll();
        System.out.println(list);
        return list;
    }

    @GetMapping("add")
    public void addRecreation(Recreation recreation, HttpServletResponse response) throws IOException {
        int r=recreationService.addRecreation(recreation);
        System.out.println(recreation);
        if(r>0){
            response.getWriter().write("ok");
            System.out.println(r);
        }else {
            response.getWriter().write("error");
        }
    }
    @GetMapping("update")
    public String update(Recreation recreation, HttpServletResponse response){
        System.out.println("=====update");
        int i=recreationService.update(recreation);
        if (i>0)return "ok";
        return "error";
    }
    @GetMapping("delete")
    public int delete(@RequestParam("rid") Integer rid){
        int a=recreationService.delete(rid);
        return a;
    }




}